class PatternsController < ApplicationController
  # GET /patterns
  # GET /patterns.xml
  def index
    @patterns = Pattern.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @patterns }
    end
  end

  # GET /patterns/1
  # GET /patterns/1.xml
  def show
    @pattern = Pattern.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @pattern }
    end
  end

  # GET /patterns/new
  # GET /patterns/new.xml
  def new
    @pattern = Pattern.new
    @patterns_phases = Array.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @pattern }
    end
  end

  # GET /patterns/1/edit
  def edit
    @pattern = Pattern.find(params[:id])
    @patterns_phases = PatternsPhase.find_all_by_pattern_id params[:id]
  end

  # POST /patterns
  # POST /patterns.xml
  def create
    
    @pattern = Pattern.new(params[:pattern])
    @patterns_phases  = Array.new
    @project_id = params[:project][:project_id]
          
    sum = 0

    #check if phases exist and sum phase weights
    if params[:phases][:id].nil?
      flash[:error] = 'No phases were added.'
      render :action => 'new' and return
    else
      params[:phases][:weight].each do |w|
        sum += w.to_i
      end
    end

    #saving user input
    params[:phases][:id].each_with_index do |phase_id,index|
      unless phase_id.empty? or phase_id.nil?
        @patterns_phase = PatternsPhase.new(:phase_id=>phase_id,:weight=>params[:phases][:weight][index])
      else
        @phase = Phase.new(:name=>params[:phases][:name][index])
        @patterns_phase = PatternsPhase.new(:weight=>params[:phases][:weight][index])
        @patterns_phase.phase = @phase
      end
      #      @patterns_phases.push({:name=>params[:phases][:name][index],:id=>phase_id,:weight=>params[:phases][:weight][index]})
      @patterns_phases.push(@patterns_phase)
    end

    #phase weights must sum to 100%
    unless sum==100
      flash[:error] = 'Phase weights must sum up to 100%'
      render :action => 'new' and return
    end

    #clear error messages
    flash.clear

    if @pattern.save
      @patterns_phases.each do |@phase|
        @phase.pattern_id = @pattern.id
        unless @phase.save
          flash[:error] = 'Phase could not be created!'
          render :action=>'new' and return
        end
      end
      flash[:notice] = 'Pattern was successfully created.'
      redirect_to :action => :index and return
    else
      render :action=>'new' and return
    end
  end

  # PUT /patterns/1
  # PUT /patterns/1.xml
  def update
    @pattern = Pattern.find(params[:id])
    @pattern.update_attributes(params[:pattern])
    @patterns_phases  = Array.new
    @project_id = params[:project][:project_id]

    sum = 0

    #check if phases exist and sum phase weights
    if params[:phases][:id].nil?
      flash[:error] = 'No phases were added.'
      render :action => 'edit' and return
    else
      params[:phases][:weight].each do |w|
        sum += w.to_i
      end
    end

    #saving user input
    params[:phases][:id].each_with_index do |phase_id,index|
      unless phase_id.empty? or phase_id.nil?
        @patterns_phase = PatternsPhase.new(:phase_id=>phase_id,:weight=>params[:phases][:weight][index])
      else
        @phase = Phase.new(:name=>params[:phases][:name][index])
        @patterns_phase = PatternsPhase.new(:weight=>params[:phases][:weight][index])
        @patterns_phase.phase = @phase
      end
      #      @patterns_phases.push({:name=>params[:phases][:name][index],:id=>phase_id,:weight=>params[:phases][:weight][index]})
      @patterns_phases.push(@patterns_phase)
    end

    #phase weights must sum to 100%
    unless sum==100
      flash[:error] = 'Phase weights must sum up to 100%'
      render :action => 'edit' and return
    end

    #clear error messages
    flash.clear

    if @pattern.save
      @pattern.patterns_phases.each do |patterns_phase|
        patterns_phase.destroy()
      end
      @patterns_phases.each do |@phase|
        @phase.pattern_id = @pattern.id
        unless @phase.save
          flash[:error] = 'Phase could not be created!'
          render :action=>'edit' and return
        end
      end
      flash[:notice] = 'Pattern was successfully updated.'
      redirect_to :action => :index and  return
    else
      render :action=>'edit' and return
    end

  end

  # DELETE /patterns/1
  # DELETE /patterns/1.xml
  def destroy
    @pattern = Pattern.find(params[:id])
    @pattern.destroy

    respond_to do |format|
      format.html { redirect_to(patterns_url) }
      format.xml  { head :ok }
    end
  end

  #Renders phase template for all phases in a pattern.
  def show_pattern_phases
    @phases = Array.new
    @phases = PatternsPhase.find_all_by_pattern_id(params[:id]) unless params[:id].empty? or  params[:id].nil?
    render :partial => 'phase_input', :locals=>{:phases=>@phases}
  end

  #Renders phase template for all phases in a project.
  def show_project_phases
    @phases = Array.new
    @phases = Activity.find_all_by_project_id(params[:id]) unless params[:id].empty? or  params[:id].nil?
    render :partial => 'phase_input', :locals=>{:phases=>@phases}
  end

  #Renders phase template.
  def add_phase
    render :partial => 'phase_input',:locals=>{:phases=>Array.new}
  end

end
